package com.beijing.http.url;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @Author Ma Wenqi
 * @Date 2021/3/28 15:17
 * @Version 1.0
 */
public class URLDemo01 {
    public static void main(String[] args) throws MalformedURLException {
        URL url = new URL("https://baike.baidu.com/item/%E7%8C%AB/22261?fr=aladdin");
        //443
        System.out.println("获取与此url关联的协议的默认端口：" + url.getDefaultPort());
        //端口号后面的内容
        //  /item/%E7%8C%AB/22261?fr=aladdin
        System.out.println("getFile:" + url.getFile());
        //baike.baidu.com
        System.out.println("主机名：" + url.getHost());
        //端口号后，参数前的内容
        //  /item/%E7%8C%AB/22261
        System.out.println("路径：" + url.getPath());
        //如果www.google.cn:80则返回80.否则返回-1
        //-1
        System.out.println("端口：" + url.getPort());
        //https
        System.out.println("协议：" + url.getProtocol());
        //fr=aladdin
        System.out.println("参数部分：" + url.getQuery());
        //null
        System.out.println("锚点：" + url.getRef());

        URL u1 = new URL("http://www.abc.com/aa/");
        URL u2 = new URL(url, "XXXXX"); // 相对路径构建url对象
        System.out.println(u2.toString()); // https://baike.baidu.com/item/%E7%8C%AB/XXXXX
    }
}
